#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<pthread.h>
#include"file.h"
#include"list.h"

int fd[2];
int start = 0, music_mute = 0 ,volume = 50, pause_f = 0;
int flag_requ_recv[2] = {0};
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_t thread_requ, thread_recv;
Music playing;

int music_player()
{
	int ch, loop = 1;
	while(loop)
	{
		ch = player_menu();
		switch(ch)
		{
			case 0: loop = 0;
					break;
			case 1:	play_pause();
					break;
			case 2: last_one();
					break;
			case 3: next_one();
					break;
			case 4: fast_forward();
					break;
			case 5: fast_rewind();
					break;
			case 6: volume_up();
					break;
			case 7: volume_down();
					break;
			case 8: mute();
					break;
		}
	}
	return 0;
}

int player_menu()
{
	int ch;
	system("clear");
	printf("\t\t\t\t=========================\n");
	printf("\t\t\t\t========Mymplayer========\n");
	printf("\t\t\t\t1.Play/Pause\n");
	printf("\t\t\t\t2.Last One\n");
	printf("\t\t\t\t3.Next one\n");
	printf("\t\t\t\t4.Fast Forward\n");
	printf("\t\t\t\t5.Fast Rewind\n");
	printf("\t\t\t\t6.Volume UP\n");
	printf("\t\t\t\t7.Volume Down\n");
	printf("\t\t\t\t8.Mute\n");
	printf("\t\t\t\t0.Quit\n");
	printf("\t\t\t\t=========================\n");
	printf("\t\t\t\tSelect:");
	scanf("%d", &ch);
	setbuf(stdin, NULL);
	while(ch < 0 || ch > 8)
	{
		printf("Error option try again:\n");
		scanf("%d", &ch);
		setbuf(stdin, NULL);
	}
	return ch;
}

void play_pause()
{
	int num;
	Music *head, *p;
	char *commad;

	head = read_list();
	printf("Please enter play id:");
	do
	{
		p = head;
		scanf("%d", &num);
		setbuf(stdin, NULL);
		while(p != NULL)
		{
			if(p -> id == num)
				break;
			p = p -> next;
		}
		if(p == NULL)
			printf("id not found\n");
	}
	while(p == NULL);

	if(start == 0)
	{
		start_mplayer(p);
		set_volume(volume);
		write_playing(p);
		start = 1;
	}
	else
	{
		if(strcmp(p -> path, playing.path) == 0)
		{
			if(pause_f)
			{
				pthread_create(&thread_requ, NULL, requ_info, NULL);
				pthread_create(&thread_recv, NULL, recv_info, NULL);
				pause_f = 0;
			}
			else
			{
				pause_f = 1;
				commad = (char *)malloc(100);
				strcpy(commad, "pause\n");
				send_message(commad);
				free(commad);
			}
		}
		else
		{
			write_playing(p);

			commad = (char *)malloc(100);
			memset(commad,0, 100);
			strcpy(commad, "loadfile ");
			strcat(commad, p -> path);
			strcat(commad, "\n");
			send_message(commad);
			free(commad);
		}
	}

	free_list(head);
}

void start_mplayer(Music *p)
{
	pid_t pid;
	char temp;
	int ret;

	pipe(fd);			//0 read ;1 write
	fcntl(fd[0], F_SETFL, O_NONBLOCK);
	mkfifo("./myfifo", O_CREAT | 0666);
	pid = fork();

	if(pid == 0)
	{
		close(fd[0]);
		dup2(fd[1],1);
		execl("/usr/bin/padsp", "padsp","mplayer", "-quiet", "-slave", 
			"-idle","-input","file=/home/ysh/Desktop/mymplayer/myfifo", p -> path, NULL);
	}
	while((ret = read(fd[0], &temp, sizeof(temp))) <= 0)
	{
		
	}
	setbuf(stdout,NULL);
	pthread_create(&thread_requ, NULL, requ_info, NULL);
	pthread_create(&thread_recv, NULL, recv_info, NULL);

}

void send_message(char *p)
{
	int fd_s;
	fd_s = open("./myfifo", O_WRONLY | O_NONBLOCK);
	if(fd_s > 0)
	{
//	printf("fd_s=%d commad=%s\n",fd_s, p);
//	printf("playing=%s\n", playing.path);
		write(fd_s, p, strlen(p));
		close(fd_s);
	}
}

int last_one()
{
	char *commad;
	Music *head, *p;

	head = read_list();
	p = head;
	if(playing.path == NULL)
	{
		printf("No music playing\n");
		return 0;
	}
	if(!strcmp(p -> path, playing.path))
	{
		while(p -> next != NULL)
			p = p -> next;
		commad = (char *)malloc(100);
		strcpy(commad, "loadfile ");
		strcat(commad, p -> path);
		strcat(commad, "\n");		
		send_message(commad);
		write_playing(p);
		free(commad);
		free_list(head);
		return 0;
	}
	else
	{
		while(p -> next != NULL)
		{
			if(!strcmp(p -> next -> path, playing.path))
				break;
			p = p -> next;
		}
	}

	if(p -> next != NULL)
	{
		commad = (char *)malloc(100);
		strcpy(commad, "loadfile ");
		strcat(commad, p -> path);
		strcat(commad, "\n");		
		send_message(commad);
		write_playing(p);
		free(commad);
		free_list(head);
		return 0;
	}

	return 0;
}

void write_playing(Music *p)
{
	playing.id = p -> id;
	strcpy(playing.name, p -> name);
	strcpy(playing.path, p -> path);
}

int next_one()
{
	char *commad;
	Music *head, *p;

	head = read_list();
	p = head;

	if(playing.path == NULL)
	{
		printf("No music playing\n");
		return 0;
	}
	while(p != NULL)
	{
		if(!strcmp(p -> path, playing.path))
			break;
		p = p -> next;
	}

	if(p -> next != NULL)
	{
		commad = (char *)malloc(100);
		strcpy(commad, "loadfile ");
		strcat(commad, p -> next -> path);
		strcat(commad, "\n");		
		send_message(commad);
		write_playing(p -> next);
		free(commad);
		free_list(head);
	}
	else
	{
		p = head;
		commad = (char *)malloc(100);
		strcpy(commad, "loadfile ");
		strcat(commad, p -> path);
		strcat(commad, "\n");		
		send_message(commad);
		write_playing(p);
		free(commad);
		free_list(head);
	}
	return 0;
}

int fast_forward()
{
	char *commad;

	if(playing.path == NULL)
	{
		printf("No music playing\n");
		return 0;
	}
	commad = (char *)malloc(100);
	strcpy(commad, "seek 10\n");	
	send_message(commad);
	free(commad);
	return 0;
}

int  fast_rewind()
{
	char *commad;

	if(playing.path == NULL)
	{
		printf("No music playing\n");
		return 0;
	}
	commad = (char *)malloc(100);
	strcpy(commad, "seek -10\n");	
	send_message(commad);
	free(commad);
	return 0;
}

int mute()
{
	char *commad;

	if(playing.path == NULL)
	{
		printf("No music playing\n");
		return 0;
	}
	if(music_mute)
	{
		commad = (char *)malloc(100);
		strcpy(commad, "mute 0\n");	
		send_message(commad);
		free(commad);
		music_mute = 0;
	}
	else
	{
		commad = (char *)malloc(100);
		strcpy(commad, "mute 1\n");	
		send_message(commad);
		free(commad);
		music_mute = 1;
	}
	return 0;
}

void set_volume(int volume_l)
{
	char *commad;
	char buf[4];

	commad = (char *)malloc(100);
	strcpy(commad, "volume ");
	sprintf(buf, "%d", volume_l);
	strcat(commad, buf);
	strcat(commad, " 1\n");
	send_message(commad);
	free(commad);
}

void volume_up()
{
	if(volume <= 90)
	{
		volume += 10;
	}
	printf("volume=%d\n", volume);
	set_volume(volume);
}

void volume_down()
{
	if(volume >= 10)
	{
		volume -= 10;
	}
	set_volume(volume);
}

void shutdown()
{
	char *commad;

	commad = (char *)malloc(100);
	strcpy(commad, "quit\n");
	send_message(commad);
	free(commad);
	wait(0);
}

void *requ_info()
{
	char *commad;

	pthread_detach(pthread_self());

	commad = (char *)malloc(100);
	while(1)
	{

		if(pause_f)
		{
			pthread_exit(NULL);
		}

		memset(commad, 0, 100);
		strcpy(commad, "get_time_pos\n");
		send_message(commad);

		flag_requ_recv[0] = 1;
		usleep(500000);

		memset(commad, 0, 100);
		strcpy(commad, "get_time_length\n");
		send_message(commad);

		flag_requ_recv[1] = 1;
		usleep(500000);
		
	}
	free(commad);
}

void *recv_info()
{
	char buf[300], *p, temp[10] = {0}, *t;
	int i, len;

	pthread_detach(pthread_self());

	while(1)
	{

		if(pause_f)
		{
			pthread_exit(NULL);
		}

		if(flag_requ_recv[1])
		{
			flag_requ_recv[1] = 0;
			read(fd[0], &buf, sizeof(buf));
			p = buf;
			while(*p != '\n')
			{
				if(!strncmp(p, "ANS_TIME_POSITION=", 18))
					break;
				p++;
			}
			if(p != NULL)
			{
				p += 18;
				t = temp;
				while(*p != '\n')
				{
					*t = *p;
					t++;
					p++;
				}
				*t = '\0';
				printf("pos=%s\n", temp);
			}
		//	printf("111111=%s\n", buf);
		}
		if(flag_requ_recv[0])
		{
			flag_requ_recv[0] = 0;
			read(fd[0], &buf, sizeof(buf));
			p = buf;
			while(*p != '\n')
			{
				if(!strncmp(p, "ANS_LENGTH=", 11))
					break;
				p++;
			}
			if(p != NULL)
			{
				p += 11;
				t = temp;
				while(*p != '.')
				{
					*t = *p;
					t++;
					p++;
				}
				*t = '\0';
				len = atoi(temp);
				printf("len = %dmin%dsec\n", len/60, len%60);
			}
		//	printf("2222222=%s\n", buf);
		}

	}
}